package ggroubig

import (
	"sync"
	"wzgames/internal/services/games"
	"wzgames/pkg/actor/actorservice"
)

// 新轮盘结算

type Counter struct {
	mu sync.Mutex
	pp *PlateMon

	roundId int64
	mp      map[uint64]*games.BetDelay
}

func NewCounter(roundId int64, pp *PlateMon) *Counter {
	return &Counter{
		roundId: roundId,
		pp:      pp,
		mp:      make(map[uint64]*games.BetDelay),
	}
}

func (ct *Counter) BetInfo(data *games.BetDelay) {
	ct.mu.Lock()
	ct.mp[data.BetObj.BetId] = data
	ct.mu.Unlock()
}

func (ct *Counter) Settle(gameId int32, data RoundCounterSettle) {
	ct.mu.Lock()
	defer ct.mu.Unlock()
	for _, v := range ct.mp {
		if v.Tb != nil && v.Tb.Player != nil {
			v.Result = data
			actorservice.Send(v.Tb.Player.Pid(), v)
		}
	}
	actorservice.Send(ct.pp.pid, RoundCounterRemove{RoundId: ct.roundId})
}

func (ct *Counter) Clear() {
	ct.mu.Lock()
	defer ct.mu.Unlock()
	ct.mp = make(map[uint64]*games.BetDelay)
}
